package cn.rslee.demo.spring.jpa.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import cn.rslee.demo.spring.jpa.entity.AccessLog;
import cn.rslee.demo.spring.jpa.repository.AccessLogRepository;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2016年2月26日      rslee         Create
 * ****************************************************************************
 * </pre>
 * @author rslee
 * @since 1.0
 */
@Component("accessLogService")
public class AccessLogServiceImpl implements AccessLogService {
	
	@Resource
	private AccessLogRepository accessLogRepository;

	@Override
	public AccessLog create(AccessLog log) {
		return accessLogRepository.save(log);
	}

	@Override
	public AccessLog update(long id, AccessLog log) {
		return accessLogRepository.save(log);
	}

	@Override
	public AccessLog patchUpdate(long id, AccessLog log) {
		AccessLog al = accessLogRepository.getOne(id);
		if(al != null){
			al.setClientIp(log.getClientIp());
			accessLogRepository.save(al);
		}
		return al;
	}

	@Override
	public List<AccessLog> findAll() {
		return accessLogRepository.findAll();
	}

	@Override
	public AccessLog get(long id) {
		return accessLogRepository.findOne(id);
	}

	@Override
	public Page<AccessLog> findByPage(int page, int size) {
		return accessLogRepository.findAll(new PageRequest(page,size));
	}

	@Override
	public AccessLog delete(long id) {
		AccessLog log = accessLogRepository.findOne(id);
		if(log != null){
			accessLogRepository.delete(id);
		}
		return log;
	}

}
